package com.kun.service_edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.service_edu.entity.EduCourse;
import com.kun.service_edu.entity.EduCourseDescription;
import com.kun.service_edu.entity.vo.CourseInfoVo;
import com.kun.service_edu.entity.vo.CoursePublishVo;
import com.kun.service_edu.entity.vo.front.CourseFrontVo;
import com.kun.service_edu.entity.vo.front.CourseWebVo;
import com.kun.service_edu.mapper.EduCourseMapper;
import com.kun.service_edu.service.EduChapterService;
import com.kun.service_edu.service.EduCourseDescriptionService;
import com.kun.service_edu.service.EduCourseService;
import com.kun.service_edu.service.EduVideoService;
import com.kun.servicebase.exceptionhandler.KunException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;
    @Override
    public String addCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表添加课程基本信息

        //courseInfoVo对象转换成eduCourse
        EduCourse eduCourse = new EduCourse();

        BeanUtils.copyProperties(courseInfoVo,eduCourse);

        int insert= baseMapper.insert(eduCourse);
        if (insert<=0){
            throw new KunException(20001,"添加失败");
        }
        //获取添加之后的课程id
        String cid = eduCourse.getId();
        //向课程表添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(cid);
        eduCourseDescriptionService.save(eduCourseDescription);
        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);
        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }
    //修改
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if (i<=0){
            throw new KunException(20001,"修改失败");
        }
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);


    }

    @Override
    public CoursePublishVo publishCourseInfo(String courseId) {
        CoursePublishVo publishCourse = baseMapper.getPublishCourseInfo(courseId);

        return publishCourse;
    }

    @Override
    public List<EduCourse> getAllCourse(Long current, Long limit, EduCourse eduCourse) {
        Page<EduCourse> page = new Page<>(current,limit);
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        String status = eduCourse.getStatus();
        String title = eduCourse.getTitle();

        Date gmtCreate = eduCourse.getGmtCreate();
        Date gmtModified = eduCourse.getGmtModified();
        if (!ObjectUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }
        if (!ObjectUtils.isEmpty(status)){
            queryWrapper.eq("status",status);
        }
        if (!ObjectUtils.isEmpty(gmtCreate)){
            queryWrapper.ge("gmtCreate",gmtCreate);
        }
        if (!ObjectUtils.isEmpty(gmtModified)){
            queryWrapper.le("gmtModified",gmtModified);
        }

        queryWrapper.orderByDesc("id");
        baseMapper.selectPage(page,queryWrapper);
        List<EduCourse> records = page.getRecords();

        return records;
    }

    @Override
    public void delCourse(String courseId) {
        //删除小节
        eduVideoService.removeByCourseId(courseId);
        //删除章节
        eduChapterService.removeByCourseId(courseId);
        //删除课程
        eduCourseDescriptionService.removeByCourseId(courseId);
        int i= baseMapper.deleteById(courseId);
        if (i <=0){
            throw new KunException(20001,"删除失败");
        }
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count",courseFrontVo.getBuyCountSort());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create",courseFrontVo.getGmtCreateSort());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price",courseFrontVo.getPriceSort());
        }
        baseMapper.selectPage(pageCourse,wrapper);
        List<EduCourse> records = pageCourse.getRecords();

        long current = pageCourse.getCurrent();
        long size = pageCourse.getSize();
        long pages = pageCourse.getPages();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();
        boolean hasPrevious = pageCourse.hasPrevious();
        HashMap<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("current",current);
        map.put("size",size);
        map.put("pages",pages);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {

        return baseMapper.getBaseCourseInfo(courseId);
    }
}
